package gang.org.springframework.framework.core;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class GangPrioritizedParameterNameDiscoverer implements GangParameterNameDiscoverer{

    public final List<GangParameterNameDiscoverer> parameterNameDiscoverers = new ArrayList<>(2);

    public void addDiscoverer(GangParameterNameDiscoverer pdn){
        this.parameterNameDiscoverers.add(pdn);
    }

    @Override
    public String[] getParameterNames(Constructor<?> ctor) {
        for (GangParameterNameDiscoverer gpnd : this.parameterNameDiscoverers) {
            String[] result = gpnd.getParameterNames(ctor);
            //无论是哪种实现，只要不为空，返回
            if (result != null) {
                return result;
            }
        }
        return null;
    }

    @Override
    public String[] getParameterNames(Method method) {

        for (GangParameterNameDiscoverer parameterNameDiscoverer : this.parameterNameDiscoverers) {

            String[] parameterNames = parameterNameDiscoverer.getParameterNames(method);

            if (parameterNames != null) {
                return parameterNames;
            }
        }

        return new String[0];
    }
}
